package dayu.service.impl;

import dayu.dao.UserDao;
import dayu.pojo.User;
import dayu.service.UserService;
import dayu.util.ShiroUtils;
import dayu.vo.PasswordVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author dayu
 * @create 2018/12/12 15:03
 * @Description
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private PasswordService passwordService;

    @Override
    public void regist(String loginName, String password) {
        PasswordVo passwordVo = new PasswordVo();
        passwordVo.setPassword(password);
        passwordVo.setSalt(loginName);
        String encryptPassword = passwordService.encryptPassword(passwordVo);
        userDao.insert(loginName, encryptPassword);
    }

    @Override
    public User findUserByLoginName(String loginName) {
        return userDao.selectUserByLoginName(loginName);
    }

    @Override
    public Set<String> findRolesByLoginName(String loginName) {
        return userDao.selectRolesByLoginName(loginName);
    }

    @Override
    public Set<String> findPermissionsByLoginName(String loginName) {
        return userDao.selectPermissionsByLoginName(loginName);
    }
}
